package org.iipg.common.data.autoconfig;

import com.google.common.base.Predicates;
import org.iipg.common.data.property.SwaggerProperties;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/** Author: zhuhy163@163.com Date: 2020/9/4 */
@Slf4j
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
@ComponentScan(basePackages = "org.iipg.common.data")
@RequiredArgsConstructor
public class SwaggerConfiguration {

	private final SwaggerProperties swaggerProperties;

	@Bean(value = "userApi")
	@ConditionalOnMissingBean(Docket.class)
	public Docket groupRestApi() {

		List<ResponseMessage> responseMessageList = new ArrayList<>();
		responseMessageList.add(new ResponseMessageBuilder().code(301).message("服务暂时不可用，请稍后重试")
				.responseModel(new ModelRef("ApiError")).build());
		responseMessageList.add(
				new ResponseMessageBuilder().code(401).message("没有权限").responseModel(new ModelRef("ApiError")).build());
		responseMessageList.add(new ResponseMessageBuilder().code(404).message("找不到资源")
				.responseModel(new ModelRef("ApiError")).build());
		responseMessageList.add(new ResponseMessageBuilder().code(409).message("业务逻辑异常")
				.responseModel(new ModelRef("ApiError")).build());
		responseMessageList.add(new ResponseMessageBuilder().code(422).message("参数校验异常")
				.responseModel(new ModelRef("ApiError")).build());
		responseMessageList.add(new ResponseMessageBuilder().code(500).message("服务器内部错误")
				.responseModel(new ModelRef("ApiError")).build());
		responseMessageList.add(new ResponseMessageBuilder().code(503).message("Hystrix异常")
				.responseModel(new ModelRef("ApiError")).build());

		Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(groupApiInfo())
				.globalResponseMessage(RequestMethod.GET, responseMessageList)
				.globalResponseMessage(RequestMethod.POST, responseMessageList).pathMapping("/")
				.select() // 选择那些路径和api会生成document
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				// .apis(RequestHandlerSelectors.any())
				.paths(Predicates.not(PathSelectors.regex("/error.*"))).paths(PathSelectors.regex("/.*")) // 对根下所有路径进行监控
				.build();
		return docket;
	}

	private ApiInfo groupApiInfo() {
		return new ApiInfoBuilder().title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription())
				.version(swaggerProperties.getVersion()).contact(new Contact(swaggerProperties.getAuthor(),
						swaggerProperties.getUrl(), swaggerProperties.getEmail()))
				.build();
	}

}
